import { observable, action } from 'mobx-miniprogram'
import { doUserInfo } from '~/api/user'
import type { userInfoState } from '~/../typings/types/stores/app'
import type { itemState, searchInfoState, cityInfoState } from '~/../typings/types/city'
const avatarImg: string = 'https://api.xzyy56.com/weixin/user/avatar.png'
import { doArea, doSiteList } from '~/api/home'
export const store = observable({
  userInfo: {
    avatarUrl: avatarImg
  } as userInfoState,
  cityInfo: {
    doArea: [],
    doSiteList: []
  } as cityInfoState,
  cityArr: [
    { "value": "320000", "label": "江苏省", "pinYin": "J", "description": "" },
    { "value": "320300", "label": "徐州市", "pinYin": "X", "description": "18913477800" }
  ] as itemState[],
  search: {
    star: '',
    end: ''
  } as searchInfoState,
  recordState: 0 as number,
  content: '' as string,

  get getUserInfo() {
    return this.userInfo;
  },
  get getRecordState() {
    return this.recordState;
  },
  get getContent() {
    return this.content;
  },
  get getCityName() {
    return this.cityArr.at(-1).label
  },
  get getCityCode() {
    return this.cityArr.at(-1).value
  },
  get getSearchInfo() {
    return this.search
  },
  initCitys() {
    doArea().then((data: any) => {
      this.cityInfo.doArea = data
    })
    doSiteList().then((data: any) => {
      this.cityInfo.doSiteList = data
    })
  },
  setUserInfo: action(async function (this: any, info: userInfoState) {
    if (!info.avatarUrl) {
      info.avatarUrl = avatarImg
    }
    this.userInfo = info
  }),
  initMusic: action(function (this: any) {
    this.recordState = 0
    this.content = ''
  }),
  setContent: action(function (this: any, e: string) {
    this.content = e
  }),
  setSearch: action(function (this: any, info: searchInfoState) {
    this.search = info
  }),
  setRecordState: action(function (this: any, e: number) {
    this.recordState = e
  }),
  doUserInfo: action(async function (this: any) {
    const data: userInfoState = await doUserInfo()
    this.setUserInfo(data)
  }),
  setCityArr: action(async function (this: any, arr: itemState[]) {
    this.cityArr = arr
  }),
  exit() {
    this.userInfo = {
      avatarUrl: avatarImg
    }
  }
});